package com.admins.config;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2022/4/30
 * 限流规则放入预加载中
 **/
@Slf4j
@Component
public class SentinelApplicationRunner implements ApplicationRunner {
    private static final String RESOURCE_KEY = "adminFlowRule";

    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<FlowRule> rules = new ArrayList<FlowRule>();
        FlowRule rule = new FlowRule();
        //不是setRefResource,别选错了
        rule.setResource(RESOURCE_KEY);
        //QPS控制在 1 以内
        rule.setCount(1);
        //QPS限流
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setLimitApp("default");
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
        log.info(">>>>"+Thread.currentThread().getName()+":...限流配置初始化成功..."+">>>>");
    }
}
